'use strict';

const Controller = require('egg').Controller;

class WebController extends Controller {

  async login() {
    const { ctx } = this;
    var data = ctx.params
    const list = await this.app.mysql.select('wx_user', { openid: data.openid });
    if (list.length == 0) {
      this.ctx.service.result.find(false)
    } else {
      this.ctx.service.result.find(list[0])
    }
  }

  async Register() {
    const { ctx } = this;
    var data = ctx.params
    console.log(data)
    // const users = await this.app.mysql.select('wx_user', { openid: data.openid });
    // if (users.length == 0) {
    await this.app.mysql.insert('wx_user', data);
    var iuser = await this.app.mysql.select('wx_user', { openid: data.openid });
    this.ctx.service.result.find(iuser[0])

    // } else {
    //   this.ctx.service.result.find(users[0])

    // }
  }

  async getUser() {
    var ctx = this.ctx
    const list = await this.app.mysql.select('wx_user'
      , { // 搜索 post 表
        limit: ctx.limit, // 返回数据量
        offset: ctx.offset, // 数据偏移量
      }
    );
    const len = await this.app.mysql.select('wx_user'
      , { // 搜索 post 表
        columns: ['id',], // 要查询的表字段
      }
    );
    this.ctx.service.result.find(list, len)

  }

  async updata() {
    var ctx = this.ctx
    const list = await this.app.mysql.update('wx_user', ctx.params);
    this.ctx.service.result.find(list)
  }


}

module.exports = WebController;
